package com.qen.epub

import cn.hutool.core.io.FileUtil
import cn.hutool.core.io.file.FileAppender
import cn.hutool.core.io.file.FileReader
import cn.hutool.core.lang.Console
import com.qen.tianya.TanxuanExcelPath
import com.qen.yanshen.ZhihuOutFile
import com.qen.yanxuan.YanxuanOutFile
import nl.siegmann.epublib.domain.Author
import nl.siegmann.epublib.domain.Book
import nl.siegmann.epublib.domain.Resource
import nl.siegmann.epublib.domain.Resources
import nl.siegmann.epublib.epub.EpubWriter
import nl.siegmann.epublib.service.MediatypeService
import java.io.File
import java.io.FileOutputStream

const val EpubOutPath = "/Users/a1/Documents/Git/Kotlin/Eleven/out/epub"

fun main() {

    val files = FileUtil.ls(ZhihuOutFile)
    val yanxuans = FileUtil.ls(YanxuanOutFile + "/20241023/txt/")
    val tianyas = FileUtil.ls(TanxuanExcelPath + "/txt/")
    val list = ArrayList<File>()
    list.addAll(files.toList())
    list.addAll(yanxuans.toList())
    list.addAll(tianyas.toList())

    Console.log(list.size)
    var resources: Resources? = null
    var book: Book? = null
    for (i in 0..list.size - 1) {
        val it = list.get(i)
        if (book == null) {
            book = Book()
            resources = Resources()
        }
        if (i % 1000 == 0) {
            book = Book()
            resources = Resources()
            book.metadata.addTitle("盐选沙雕文章")
            book.metadata.addAuthor(Author("MayBeTheGo"))
        }
        if (it.isFile && it.name.endsWith("txt")) {
            val reader = FileReader(it.path)
            val title = removeNoZhCn(it.name)
//            val template = """
//                <?xml version="1.0" encoding="UTF-8"?>
//                <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
//                <html xmlns="http://www.w3.org/1999/xhtml">
//                <head>
//                    <title>My First EPUB Book</title>
//                </head>
//                <body>
//                    <h1>Chapter 1</h1>
//                    <p>This is the content of Chapter 1.</p>
//                </body>
//                </html>
//            """.trimIndent()
            val p = StringBuilder()
            reader.readLines { line ->
                p.append(
                    """
                    <p>$line</p>
                """.trimIndent()
                )
            }
            val template = """
                <?xml version="1.0" encoding="UTF-8"?>
                <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
                <html xmlns="http://www.w3.org/1999/xhtml">
                <head>
                    <title>${title}</title>
                </head>
                <body>
                    <h1>${title}</h1>
                    $p
                </body>
                </html>
            """.trimIndent()
//            Console.log(template)
            val chapter1Resource = Resource(template.toByteArray(), "${System.currentTimeMillis()}.xhtml")
            chapter1Resource.mediaType = MediatypeService.XHTML
            resources?.add(chapter1Resource)
            book.addSection(title, chapter1Resource)
//            book.spine.addSpineReference(SpineReference(chapter1Resource))
        }
        Console.log("$i:${it.name}:${it.length()}")
        if (i % 1000 == 999 || i == list.size - 1) {
            book.resources = resources
            val outputFile = File("$EpubOutPath/盐选沙雕文章${i / 1000}.epub")
            val outputStream = FileOutputStream(outputFile)
            EpubWriter().write(book, outputStream)
            outputStream.close()
            Console.log("$EpubOutPath/盐选沙雕文章${i / 1000}.epub")
        }
    }
    Console.log("End")
}

fun removeNoZhCn(input: String): String {
    val pattern = "\\P{IsHan}+".toRegex()
    val result = input.replace(pattern, "")
    return result
}

//fun main() {
//    println(removeNoZhCn("12312faf安徽大黄蜂v，备注陈老师大"))
//}